//
//  MYGSystemMessageTypeCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/2/29.
//

import UIKit
import QMUIKit

class MYGSystemMessageTypeCell: UICollectionViewCell {
    // MARK: - Property
    var model: MYGSystemTypeCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            button.setImage(UIImage(named: "Chat/Type/\(model.image)"), for: .normal)
            button.setTitle(model.name, for: .normal)
            bgView.isHidden = !model.isSel
            numLabel.isHidden = true
            if model.num != 0 {
                numLabel.isHidden = false
                numLabel.text = "\(model.num)"
            }
        }
    }
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
        numLabel.isHidden = true
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(bgView)
        contentView.addSubview(button)
        contentView.addSubview(numLabel)
        
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        button.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        numLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.height.width.equalTo(14)
            make.centerX.equalToSuperview().offset(10)
        }
        
    }
    // MARK: - lazy load
    private lazy var button: QMUIButton = {
        let btn = QMUIButton()
        btn.imagePosition = .top
        btn.spacingBetweenImageAndTitle = 5
        btn.setTitleColor(.rgba(44, 44, 44, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    private lazy var bgView: UIView = {
        let view = UIView()
        let gl = CAGradientLayer()
        gl.frame = CGRect(x: 0, y: 0, width: 72, height: 85)
        gl.locations = [0, 1]
        gl.colors = [UIColor.rgba(255, 254, 251, 1).cgColor, UIColor.rgba(254, 227, 180, 0.23).cgColor]
        gl.startPoint = CGPoint(x: 0.5, y: 0)
        gl.endPoint = CGPoint(x: 1, y: 1)
        gl.masksToBounds = true
        gl.cornerRadius = 5
        view.layer.addSublayer(gl)
        
        return view
    }()
    
    private lazy var numLabel: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.font = .regular(9)
        label.textAlignment = .center
        
        label.backgroundColor = .red
        label.layer.cornerRadius = 7
        label.layer.masksToBounds = true
        label.text = ""
        return label
    }()
}
